package servlets;

import dao.JdbcUserDao;
import entries.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RemoveUser extends Dispatcher {

    private final static Log LOGGER = LogFactory.getLog(RemoveUser.class);

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
            IOException {

        HttpSession session = req.getSession();

        JdbcUserDao userDao = new JdbcUserDao();
        long id = Long.parseLong(req.getParameter("id"));

        try {
            User user = new User();
            user.setId(id);
            userDao.remove(user);

            List<User> list = userDao.findAll();
            req.setAttribute("users", list);

            if ("Admin".equals(req.getAttribute("role"))){
                this.forward("/login.jsp", req, resp);
                session.invalidate();
                return;
            } else {
                this.forward("/adminPage.jsp", req, resp);
                return;
            }

        } catch (SQLException e) {
            LOGGER.error("SQLException", e);
        } catch (ClassNotFoundException e) {
            LOGGER.error("ClassNotFoundException", e);
        }


    }
}

